package com.syqc.monitor.ui.cost

import autodispose2.autoDispose
import com.syqc.comlib.auto.AutoDisposeViewModel
import com.syqc.entity.DriverInfo
import com.syqc.monitor.R
import com.syqc.net.ServiceHelper
import io.reactivex.rxjava3.core.Observable
import io.reactivex.rxjava3.subjects.PublishSubject

/**
 *@Author Jasper
 *@Time   2020/12/16
 *@Desc   司机
 */
class DriversViewModel : AutoDisposeViewModel() {
    private var drivers: MutableList<DriverInfo>? = null

    private val dataSub by lazy { PublishSubject.create<MutableList<DriverInfo>>() }
    fun dataObserver(): Observable<MutableList<DriverInfo>> = dataSub.hide()

    fun getDriversData() {
        toastLoading(true)
        ServiceHelper.getRepository().getDriversData().autoDispose(this).subscribe({
            toastLoading(false)
            drivers = it
            if (it.isNullOrEmpty()) {
                toast(R.string.toast_plase_add_driver)
            }
            dataSub.onNext(it)
        }, error)
    }

    fun searchDrivers(input: String) {
        if (drivers == null) return
        val searchData = ArrayList<DriverInfo>()
        for (entity in drivers!!) {
            if (entity.name.contains(input)) searchData.add(entity)
        }
        dataSub.onNext(searchData)
    }
}
